package airthmetic.exercise.linkedList;

public class _206_反转链表 {
    // 递归写法
    public static ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode n = reverseList(head.next);
        head.next.next = head;
        head.next = null;

        return n;
    }

    // 循环写法
    public static ListNode reverseList2(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode prev = null;
        ListNode current = null;
        while(head != null && head.next != null){
            current = head;
            head = head.next;
            current.next = prev;
            prev = current;
        }
        return prev;


    }

    public static void main(String[] args) {
        ListNode listNode4 = new ListNode(4, null);

        ListNode listNode3 = new ListNode(3, listNode4);
        ListNode listNode2 = new ListNode(2, listNode3);
        ListNode listNode = new ListNode(1, listNode2);


        System.out.println(reverseList(listNode));
    }
}
